package com.xuzao.developer.dingding;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class RebootUtil {

    /**
     * 组装 发送的信息
     *
     * @param isAt       是否需要 @所有人
     * @param msgContent 要发送信息的主体
     * @return
     */
    public static void setMessage(boolean isAt, String msgContent,String phones) {
        String url = "https://oapi.dingtalk.com/robot/send?access_token=f252941446df7edc44e5dd9b1c917f4cc59b16ab1398c9f178603532ee3b56c1";
        TextRebootModel model = new TextRebootModel();
        AtMobiles atMobiles = new AtMobiles();
        atMobiles.setIsAtAll(isAt);
        //要@人的手机号码，默认不@
        if(StringUtils.isBlank(phones)){
            atMobiles.setAtMobiles(Arrays.asList(""));
        }else{
            atMobiles.setAtMobiles(Arrays.asList(phones));
        }

        ContentModel contentModel = new ContentModel();
        contentModel.setContent(msgContent);

        model.setAt(atMobiles);
        model.setText(contentModel);

        String toMsg = JSON.toJSONString(model);

        try {
            RebootUtil.sendErrorReboot(toMsg,url);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 组装 发送的信息
     *
     * @param isAt       是否需要 @所有人
     * @param title      标题
     * @param msgContent 要发送信息的主体
     * @param telList    要 @人的电话号码,如果@单独的几个人，就传一个空list，而不是 null
     * @return
     */
    public static void setMarkDown(boolean isAt, String title, String msgContent, List<String> telList,String url) {

        MarkDownRebootModel model = new MarkDownRebootModel();
        AtMobiles atMobiles = new AtMobiles();
        atMobiles.setIsAtAll(isAt);
        atMobiles.setAtMobiles(telList);

        MarkDownModel markDownModel = new MarkDownModel();
        markDownModel.setTitle(title);
        markDownModel.setText(msgContent);

        model.setAt(atMobiles);
        model.setMarkdown(markDownModel);

        String message = JSON.toJSONString(model);

        try {
            RebootUtil.sendErrorReboot(message,url);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * post 请求，发送给哪一个机器人
     *
     * @param reboot  机器人的token
     * @param message 发送的消息
     * @return
     */
    private static String sendPost(String reboot, String message) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(reboot);
        httppost.addHeader("Content-Type", "application/json; charset=utf-8");

        String textMsg = message;
        StringEntity se = new StringEntity(textMsg, "utf-8");
        httppost.setEntity(se);
        String result = null;
        HttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }


    /**
     * 查错机器人
     * @param message 封装的消息
     * @return
     * @throws Exception
     */
    private static String sendErrorReboot(String message,String url) {
        return sendPost(url, message);
    }


}
